package com.chaodev.base.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.ResourceListDao;
import com.chaodev.base.dto.ResourceListDto;
import com.chaodev.base.model.ResourceList;
import com.chaodev.base.service.ResourceListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResourceListServiceImpl implements ResourceListService {

    @Autowired
    private ResourceListDao resourceListDao;

    @Override
    public boolean saveResourceList(ResourceList resourceList) {
        boolean flag;
        if (ObjectUtil.isNotNull(resourceList.getId())&& !"".equals(resourceList.getId())){
            flag = resourceListDao.updateResourceList(resourceList) != 0;
        }else {
            flag = resourceListDao.insertResourceList(resourceList) != 0;
        }
        return flag;
    }

    @Override
    public boolean addResourceList(ResourceList resourceList) {
        return resourceListDao.insertResourceList(resourceList) != 0;
    }

    @Override
    public boolean editResourceList(ResourceList resourceList) {
        return resourceListDao.updateResourceList(resourceList) != 0;
    }

    @Override
    public boolean deleteResourceList(ResourceList resourceList) {
        return resourceListDao.deleteResourceList(resourceList.getId()) != 0;
    }

    @Override
    public List<ResourceList> getResourceLists(ResourceListDto resourceListDto) {
        return resourceListDao.selectResourceLists(resourceListDto);
    }

    @Override
    public PageInfo<ResourceList> getResourceListPage(ResourceListDto resourceListDto) {
        PageHelper.startPage(resourceListDto.getPage(),resourceListDto.getRows());
        List<ResourceList> list = resourceListDao.selectResourceLists(resourceListDto);
        PageInfo<ResourceList> pageInfo = new PageInfo<>(list, resourceListDto.getRows());
        return pageInfo;
    }

    @Override
    public ResourceList getResourceListById(ResourceList resourceList) {
        return resourceListDao.selectResourceListById(resourceList.getId());
    }

    @Override
    public void updateVisitNum(ResourceList resourceList) {
        ResourceList current = resourceListDao.selectResourceListById(resourceList.getId());
        Long visitNum = current.getVisitNum() == null ? 0 : current.getVisitNum();
        resourceList.setVisitNum(visitNum + 1);
        resourceListDao.updateVisitNum(resourceList);
    }


}
